<template>
  <div class="home-search">
    <form class="form">
      <button>
        <svg width="17" height="16" fill="none" xmlns="http://www.w3.org/2000/svg" role="img" aria-labelledby="search">
          <path d="M7.667 12.667A5.333 5.333 0 107.667 2a5.333 5.333 0 000 10.667zM14.334 14l-2.9-2.9" stroke="currentColor" stroke-width="1.333" stroke-linecap="round" stroke-linejoin="round"></path>
        </svg>
      </button>
      <input class="input" placeholder="搜索服务" type="text" v-model="inputValue" @blur="resultShow = false" @focus="onFocus">
      <button class="reset" type="reset" @click="handleClear">
        <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
          <path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12"></path>
        </svg>
      </button>
      <div class="result" v-show="resultShow">
        <el-scrollbar height="400px">
          <ul v-if="serverList.length > 0" class="search-result" v-loading="searchLoading">
            <li class="search-result-item" v-for="(item,index) in serverList" :key="index">
              <div class="logo">
                <img class="logo-img" :src="item.logo" alt="">
              </div>
              <div class="name">{{ item.name }}</div>
            </li>
          </ul>
          <el-empty v-else description="没有搜索"></el-empty>
        </el-scrollbar>
      </div>
    </form>
  </div>
</template>

<script setup>
/* eslint-disable */
import { ref, watch } from 'vue'
import { getServerList } from '@/api/server'

// 获取输入框的值
const serverList = ref([])
const searchLoading = ref(false)
const resultShow = ref(false)
const _getServerList = async (val) => {
  searchLoading.value = true
  try{
    const res = await getServerList({ name: val})
    if(res.code === 20000){
      serverList.value = res.data.list
    }
  }catch{
    serverList.value = []
  }
  searchLoading.value = false
}
const inputValue = ref('')
watch(() => inputValue.value, (val) => {
  if(!val){
    serverList.value = []
    resultShow.value = false
    return
  }
  resultShow.value = true
  _getServerList(val)
})
// 清除
const handleClear = () => {
  inputValue.value = ''
}
// 聚焦
const onFocus = () => {
  if(inputValue.value){
    resultShow.value = true
  }
}
</script>

<style lang='scss' scoped>
.home-search {
  // position: relative;
  .result{
    position: absolute;
    z-index: 1;
    top: 55px;
    left: 0;
    right: 0;
    padding: 10px;
    background: rgba(243, 244, 244, 0.9);
    .search-result{
      padding: 0;
      .search-result-item{
        display: flex;
        flex-wrap: nowrap;
        align-items: center;
        overflow: hidden;
        padding: 0 12px;
        margin-bottom: 4px;
        width: 100%;
        height: 56px;
        border-radius: 4px;
        font-size: 14px;
        font-weight: 600;
        box-sizing: border-box;
        box-shadow: 0 1px 3px 0 #d4d9e1;
        background: #ffffff;
        .logo{
          margin-right: 10px;
          width: 40px;
          height: 40px;
          border-radius: 4px;
          overflow: hidden;
          img{
            width: 100%;
            height: 100%;
          }
        }
      }
      .search-result-item:hover{
        background: #409EFF;
        color: #ffffff;
        cursor: pointer;
      }
    }
  }
}
.form button {
  border: none;
  background: none;
  color: #8b8ba7;
}
/* styling of whole input container */
.form {
  --timing: 0.3s;
  --width-of-input: 500px;
  --height-of-input: 50px;
  --border-height: 2px;
  --input-bg: #fff;
  --border-color: #409EFF;
  --border-radius: 30px;
  --after-border-radius: 1px;
  position: relative;
  width: var(--width-of-input);
  height: var(--height-of-input);
  display: flex;
  align-items: center;
  padding-inline: 0.8em;
  border-radius: var(--border-radius);
  transition: border-radius 0.5s ease;
  background: var(--input-bg,#eee);
}
/* styling of Input */
.input {
  font-size: 0.9rem;
  background-color: transparent;
  width: 100%;
  height: 100%;
  padding-inline: 0.5em;
  padding-block: 0.7em;
  border: none;
}
/* styling of animated border */
.form:before {
  content: "";
  position: absolute;
  background: var(--border-color);
  transform: scaleX(0);
  transform-origin: center;
  width: 100%;
  height: var(--border-height);
  left: 0;
  bottom: 0;
  border-radius: 1px;
  transition: transform var(--timing) ease;
}
/* Hover on Input */
.form:focus-within {
  border-radius: var(--after-border-radius);
}

input:focus {
  outline: none;
}
/* here is code of animated border */
.form:focus-within:before {
  transform: scale(1);
}
/* styling of close button */
/* == you can click the close button to remove text == */
.reset {
  border: none;
  background: none;
  opacity: 0;
  visibility: hidden;
}
/* close button shown when typing */
input:not(:placeholder-shown) ~ .reset {
  opacity: 1;
  visibility: visible;
}
/* sizing svg icons */
.form svg {
  width: 17px;
  margin-top: 3px;
}
</style>